﻿
/// <reference path="../Popup/ShowListAdmin.js" />

//-----------------------------------------------------------
// Description: Xử lý các action trong trang quản lý Role
//-----------------------------------------------------------
pages_admin_Role = new function () {

    this.m_roleID = 0;

    // type=0: Thêm
    // type=1: Sửa
    this.m_type = 0;

    this.documentReady = function () {
        pages_admin_Role.binDataListRole();
    };

    this.binDataListRole = function () {
        Loading.showProcess();
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { t: 'GetListRole' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#divListRole").setTemplateURL('/Template/Pages/Config/ListRole.htm');
                $("#divListRole").processTemplate(data);
                pages_admin_Role.setRowNumber("#divListRole");
                pages_admin_Role.setStyleRow("#divListRole");
                Loading.closeProcess();
            }
        });
    };

    this.setRowNumber = function (m_selParent) {
        var m_stt = 0;
        $('#divSTT', m_selParent).each(function () {
            m_stt++;
            $(this).text(m_stt);
        });
    }

    $("#btnAddRole").live('click', function () {
        pages_admin_Role.m_type = 0;
        $("#divList").slideUp(0, function () {
            $("#divInfoRole").slideDown(0);
        });
        $("#txtRoleName").val('');
        $("#txtRoleName").focus();
        $("#txtRoleDescription").val('');

        $(".optStatusRole").remove();
        for (var i = 1; i < 3; i++) {
            $("#ddlRoleStatus").append('<option value= ' + i + ' class="optStatusRole" >' + pages_admin_Role.convertStatus(i) + '</option>');
        }
    });

    this.bindInfoRole = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { roleID: pages_admin_Role.m_roleID, t: 'GetRoleInfo' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#txtRoleName").val(data.Name);
                $("#txtRoleName").focus();
                $("#txtRoleDescription").val(data.Description);

                $(".optStatusRole").remove();
                for (var i = 1; i < 3; i++) {
                    if (i == data.Status) {
                        $("#ddlRoleStatus").append('<option value= ' + i + ' selected="selected" class="optStatusRole" >' + pages_admin_Role.convertStatus(i) + '</option>');
                    }
                    else {
                        $("#ddlRoleStatus").append('<option value= ' + i + ' class="optStatusRole" >' + pages_admin_Role.convertStatus(i) + '</option>');
                    }
                }
            }
        });
    };

    this.bindPermission = function () {
        pages_admin_Role.loadMenuInRole();
        pages_admin_Role.loadMenuNotInRole();
    };

    this.loadMenuNotInRole = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { roleID: pages_admin_Role.m_roleID, t: 'GetListMenuNotInRole' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#ulFunctionNotInRole").setTemplateURL('/Template/Pages/Config/ListMenuNotInRole.htm');
                $("#ulFunctionNotInRole").processTemplate(data);
                for (var i = 0; i < data.Items.length; i++) {
                    var _sHtml = "";
                    var _data = data.Items[i].Items;
                }
                pages_admin_Role.bindTreeview("#ulFunctionInRole");
                Loading.closeProcess();
            }
        });
    };

    this.loadMenuInRole = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { roleID: pages_admin_Role.m_roleID, t: 'GetListMenuInRole' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#ulFunctionInRole").setTemplateURL('/Template/Pages/Config/ListMenuInRole.htm');
                $("#ulFunctionInRole").processTemplate(data);
                pages_admin_Role.bindTreeview("#ulFunctionInRole");
                Loading.closeProcess();
            }
        });
    };

    this.bindTreeview = function (m_sel) {
        // click vào functionparent
        $(".spNameFunctionParent").live('click', function () {
            if ($("ul", this.parentNode).attr("class") == "visible") {
                $("ul", this.parentNode).slideUp(0, function () {
                    $(this).attr("class", "invisible");
                    $(this.parentNode).attr('class', 'expandable');
                });

            }
            else {
                $("ul", this.parentNode).slideDown(0, function () {
                    $(this).attr("class", "visible");
                    $(this.parentNode).attr('class', 'collapsable');
                });

            }
        });

        // click vào checkbox functionparent
        $(".cbxFunctionParent").live("click", function () {
            if (this.checked) {
                $("li", this.parentNode).find("input[type='checkbox']").attr("checked", "" + this.checked);
                $("ul", this.parentNode).slideDown(0, function () {
                    $(this).attr("class", "visible");
                    $(this.parentNode).attr('class', 'collapsable');
                });
            }
            else {
                $("li", this.parentNode).find("input[type='checkbox']").attr("checked", "");
            }
        });

        // click vào checkbox function
        $(".cbxFunctionNotInRole").live("click", function () {
            if (this.checked) {
                $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "true");
            }
            else {
                if ($(this.parentNode.parentNode).find("input[checked='true']").length == 0) {
                    $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "");
                }
            }
        });

        // click vào checkbox function
        $(".cbxFunctionInRole").live("click", function () {
            if (this.checked) {
                $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "true");
            }
            else {
                if ($(this.parentNode.parentNode).find("input[checked='true']").length == 0) {
                    $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "");
                }
            }
        });
    };

    $("#btnAddFunctionToRole").live('click', function () {
        var m_functionIDs = "";
        $(".cbxFunctionNotInRole:checked").each(function () {
            var m_functionID = $(this).attr("id").substring(3);
            m_functionIDs += m_functionID + ",";
        });
        if (m_functionIDs == "") {
            jAlert(MESSAGE.errorCheck, MESSAGE.title);
            return;
        }
        m_functionIDs = m_functionIDs.substring(0, m_functionIDs.lastIndexOf(","));
        //add nhiều function vào role
        var _data = {
            roleID: pages_admin_Role.m_roleID,
            menuIDs: m_functionIDs
        };
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/AddRoleFunctionMulti",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.loadMenuInRole();
                pages_admin_Role.loadMenuNotInRole();
            }
        });

    });

    $("#btnDeleteFunctionFromRole").live('click', function () {
        var m_functionIDs = "";
        $(".cbxFunctionInRole:checked").each(function () {
            var m_functionID = $(this).attr("id").substring(3);
            m_functionIDs += m_functionID + ",";
        });
        if (m_functionIDs == "") {
            jAlert(MESSAGE.errorCheck, MESSAGE.title);
            return;
        }
        m_functionIDs = m_functionIDs.substring(0, m_functionIDs.lastIndexOf(","));

        //Xóa nhiều function khỏi Role
        var _data = {
            roleID: pages_admin_Role.m_roleID,
            menuIDs: m_functionIDs
        };
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/DeleteRoleFunctionMulti",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.loadMenuInRole();
                pages_admin_Role.loadMenuNotInRole();
            }
        });
    });

    $("#aViewDetailRole").live('click', function () {
        pages_admin_Role.displayControl(false);
        Loading.showProcess();
        pages_admin_Role.m_type = 1;
        pages_admin_Role.m_roleID = $(this.parentNode).attr("id").substring(4);
        pages_admin_Role.bindInfoRole();
        pages_admin_Role.bindPermission();
        pages_admin_Role.bindUserInRole();
        Loading.closeProcess();

    });

    $("#btnUpdateRoleInfo").live('click', function () {
        Loading.showProcess();
        if ($("#txtRoleName").val() == '') {
            Loading.closeProcess();
            jAlert(MESSAGE.errorInfo, MESSAGE.title);
            return;
        }

        if (pages_admin_Role.m_type == 0) {
            var _data = {
                name: $("#txtRoleName").val(),
                description: $("#txtRoleDescription").val(),
                status: $("#ddlRoleStatus").val()
            };
            $.ajax({
                type: "POST",
                url: "/WebService/WSConfig.asmx/AddRole",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    Loading.closeProcess();
                    if (data.d > 0) {
                        
                        jAlert(MESSAGE.addSuccess, MESSAGE.title);
                        pages_admin_Role.binDataListRole();
                        pages_admin_Role.displayControl(true);
                    }
                    else {
                        jAlert(MESSAGE.error, MESSAGE.title);
                    }

                }
            });
        }
        else {
            var _data = {
                roleID: pages_admin_Role.m_roleID,
                name: $("#txtRoleName").val(),
                description: $("#txtRoleDescription").val(),
                status: $("#ddlRoleStatus").val()

            };
            $.ajax({
                type: "POST",
                url: "/WebService/WSConfig.asmx/UpdateRoleInfo",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    Loading.closeProcess();
                    if (data.d > 0) {
                        jAlert(MESSAGE.updateSuccessRoleInfo, MESSAGE.title);
                        pages_admin_Role.binDataListRole();
                        pages_admin_Role.displayControl(true);
                    }
                    else {
                        jAlert(MESSAGE.error, MESSAGE.title);
                    }
                }
            });
        }
    });

    $("#aUpdatePermission").live('click', function () {
        Loading.showProcess();
        var _functionID = $(this.parentNode).attr("id").substring(2);
        var _per = 0;
        if ($("#cbxPermissionView" + _functionID, this.parentNode).attr('checked')) {
            _per += 8;
        }
        if ($("#cbxPermissionAdd" + _functionID, this.parentNode).attr('checked')) {
            _per += 4;
        }
        if ($("#cbxPermissionEdit" + _functionID, this.parentNode).attr('checked')) {
            _per += 2;
        }
        if ($("#cbxPermissionDelete" + _functionID, this.parentNode).attr('checked')) {
            _per += 1;
        }

        var _data = {
            roleID: pages_admin_Role.m_roleID,
            menuID: _functionID,
            permission: _per
        }
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/UpdatePermissionRoleFunction",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.loadMenuInRole();
                jAlert(MESSAGE.updatePermissionRoleFunctionSuccess, MESSAGE.title);
                Loading.closeProcess();
            }
        });
    });

    $("#btnCancelRoleInfo").live('click', function () {
        pages_admin_Role.displayControl(true);
    });

    $("#aDeleteRole").live('click', function () {
        Loading.showProcess();
        pages_admin_Role.m_roleID = $(this.parentNode).attr("id").substring(6);
        var _data = {
            roleID: pages_admin_Role.m_roleID
        };
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/DeleteRole",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data.d > 0) {
                    jAlert(MESSAGE.deleteSuccess, MESSAGE.title);
                    pages_admin_Role.binDataListRole();
                }
                else {
                    jAlert(MESSAGE.error, MESSAGE.title);
                }
                Loading.closeProcess();
            }
        });
    });

    this.displayControl = function (_bool) {
        if (_bool) {
            $("#divRoleUser").slideUp(0, function () {
                $("#divRolePermission").slideUp(0, function () {
                    $("#divInfoRole").slideUp(0, function () {
                        $("#divList").slideDown(0);
                    });
                });
            });
        }
        else {
            $("#divList").slideUp(0, function () {
                $("#divInfoRole").slideDown(0, function () {
                    $("#divRolePermission").slideDown(0, function () {
                        $("#divRoleUser").slideDown(0);
                    });
                });
            });
        }
    }

    this.convertStatus = function (_status) {
        if (_status == 1)
            return MESSAGE.status_active;
        if (_status == 2)
            return MESSAGE.status_lock;
    }

    $("#aAddFunction").live('click', function () {
        Loading.showProcess();
        pages_admin_Role.m_functionID = $(this.parentNode).attr("id").substring(2);
        var _data = {
            roleID: pages_admin_Role.m_roleID,
            menuID: pages_admin_Role.m_functionID
        }
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/AddRoleFunction",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.loadMenuInRole();
                pages_admin_Role.loadMenuNotInRole();
                Loading.closeProcess();
            }
        });
    });

    $("#aDeleteFunction").live('click', function () {
        Loading.showProcess();
        pages_admin_Role.m_functionID = $(this.parentNode).attr("id").substring(2);
        var _data = {
            roleID: pages_admin_Role.m_roleID,
            menuID: pages_admin_Role.m_functionID
        }
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/DeleteRoleFunction",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.loadMenuInRole();
                pages_admin_Role.loadMenuNotInRole();

            }
        });
    });

    this.bindUserInRole = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { roleID: pages_admin_Role.m_roleID, t: 'GetListAdminInRole' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#divListUserInRole").setTemplateURL('/Template/Pages/Config/ListAdminInRole.htm');
                $("#divListUserInRole").processTemplate(data);
                pages_admin_Role.setStyleRow("#divListUserInRole");
                pages_admin_Role.setRowNumber("#divListUserInRole");
            }
        });
    }

    $("#aDeleteAdminFromRole").live('click', function () {
        Loading.showProcess();
        var _perID = $(this.parentNode).attr("id").substring(6);
        var _data = {
            permisionID: _perID
        }
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/DeletePermission",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Role.bindUserInRole();
                jAlert(MESSAGE.deleteSuccess, MESSAGE.title);
                Loading.closeProcess();
            }
        });
    });

    $("#aOpenPopupListAdmin").live('click', function () {
        ShowListAdmin.showPopup(pages_admin_Role.m_roleID);
    });


    this.setStyleRow = function (m_selParent) {
        var m_stt = 0;
        $('#divItem', m_selParent).each(function () {
            m_stt++;
            if ((m_stt % 2) == 0) {
                $(this.parentNode).addClass('even');
            }
            else {
                $(this.parentNode).addClass('odd');
            }
        });
    };
}